package org.free.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;

public class Tags {

	public static Key setTag(Key person, String tag1) {
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();

		/*
		 * q.addFilter("name", Query.FilterOperator.EQUAL, name);
		 * q.addFilter("password", Query.FilterOperator.EQUAL, password);
		 * PreparedQuery pq = datastore.prepare(q); for (Entity result :
		 * pq.asIterable()) { return result.getKey(); }
		 */
		// Entity person = new Entity("Person");
		Entity tag = new Entity("Tag");
		tag.setProperty("tag", tag1);
		tag.setProperty("person", person);

		return datastore.put(tag);

	}

	public static List<Key> getUsers(String searchText) {
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		List<Key> l = new ArrayList<Key>();
		Query q = new Query("Tag");
		q.addFilter("tag", Query.FilterOperator.EQUAL, searchText);
		PreparedQuery pq = datastore.prepare(q);

		for (Entity result : pq.asIterable()) {
			l.add((Key) result.getProperty("person"));
		}
		return l;

	}
}
